﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OtherDevice
{
    [Description("新大陆扫码器-430")]
    public class Scan_NLSFM430 : SNScan_Base
    {
        private List<string> list_SN = new List<string>();
        private object obj = new object();
        public Scan_NLSFM430(string strName) : base(strName)
        {
            controlFrom = new Form_扫码();
        }
        public override void InitDeal()
        {
            m_SerialPort.DataReceived += M_SerialPort_DataReceived;
        }
        int n_Length = 0;
        private void M_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string strRecivSN = "";
            byte[] byte_Result = new byte[n_Length];
            ReceiveData(ref byte_Result, 3000);
            strRecivSN = Encoding.Default.GetString(byte_Result);
            string[] str_SN = strRecivSN.Split('\r');  //扫码枪后缀设置回车换行
            strRecivSN = str_SN[0].Replace("\u001e", "");  //字母息分隔符二 \u001e
            strRecivSN = strRecivSN.Replace("\u0006", "");  //确认 \u0006
            strRecivSN = strRecivSN.Replace("\u001d", "");  //信息分隔符三 \u001d
            strRecivSN = strRecivSN.Replace("\u0004", "");    //传输结束 \u0004
            strRecivSN = strRecivSN.Replace("\0", "");  //结束 \0
            strRecivSN = strRecivSN.Replace("\r", "");  //回车 \r
            strRecivSN = strRecivSN.Replace("\n", "");  //换行 \n
            list_SN.Clear();
            list_SN.Add(strRecivSN);
        }

        public override bool Function_触发扫码(int n_SnLength, ref string strSN, int n_Timeout = 2000)
        {
            lock (obj)
            {
                try
                {
                    n_Length = n_SnLength + 1;
                    byte[] b_Value = new byte[2];
                    b_Value[0] = 0x1b;
                    b_Value[1] = 0x31;
                    list_SN.Clear();
                    m_SerialPort.DiscardInBuffer();
                    SendData(b_Value);
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    while (true)
                    {
                        if (watch.ElapsedMilliseconds > n_Timeout)
                        {
                            return false;
                        }
                        if (list_SN.Count >= 1)
                        {
                            strSN = list_SN[0];
                            if (strSN != null)
                            {
                                if (strSN.Length == n_SnLength)
                                    return true;
                                else
                                    return false;
                            }
                            else
                            {
                                strSN = "";
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (GlobalVariable.g_StationState == StationState.StationStateStop)
                        MessageBox.Show(new Form { TopMost = true }, $"读取扫码数据异常\n错误信息={ex.Message}\n堆栈信息={ex.StackTrace}");
                    return false;
                }
            }
        }

        public override int Fucntion_获取SN码(int n_SnLength, ref string strSN, int n_Timeout = 2000)
        {
            lock (obj)
            {
                try
                {
                    n_Length = n_SnLength + 1;
                    list_SN.Clear();
                    m_SerialPort.DiscardInBuffer();
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    while (true)
                    {
                        if (n_Timeout > 0 && stopwatch.ElapsedMilliseconds > n_Timeout)
                        {
                            strSN = "";
                            return -2;
                        }
                        if (list_SN.Count >= 1)
                        {
                            strSN = list_SN[0];
                            if (strSN != null)
                            {
                                if (strSN.Length == n_SnLength)
                                    return 0;
                                else
                                    return -1;
                            }
                            else
                            {
                                strSN = "";
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (GlobalVariable.g_StationState == StationState.StationStateStop)
                        MessageBox.Show(new Form { TopMost = true }, $"读取扫码数据异常\n错误信息={ex.Message}\n堆栈信息={ex.StackTrace}");
                    return -2;
                }
            }
        }
    }
}